<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="活动日期：">
						<div style="display: flex; align-items: center">
							<a-form-item style="flex: 1; margin-right: 12px" name="promoteStartTime">
								<a-date-picker
									placeholder="请选择开始日期"
									class="block"
									format="YYYY-MM-DD"
									v-model:value="searchFormState.promoteStartTime"
									allow-clear
								/>
							</a-form-item>
							<a-form-item style="flex: 1" name="promoteEndTime">
								<a-date-picker
									placeholder="请选择结束日期"
									class="block"
									format="YYYY-MM-DD"
									v-model:value="searchFormState.promoteEndTime"
									allow-clear
								/>
							</a-form-item>
						</div>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="销售日期：">
						<div style="display: flex; align-items: center">
							<a-form-item style="flex: 1; margin-right: 12px" name="saleStartTime">
								<a-date-picker
									placeholder="请选择开始日期"
									class="block"
									format="YYYY-MM-DD"
									v-model:value="searchFormState.saleStartTime"
									allow-clear
								/>
							</a-form-item>
							<a-form-item style="flex: 1" name="saleEndTime">
								<a-date-picker
									placeholder="请选择结束日期"
									class="block"
									format="YYYY-MM-DD"
									v-model:value="searchFormState.saleEndTime"
									allow-clear
								/>
							</a-form-item>
						</div>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="活动状态：" name="status">
						<a-select
							v-model:value="searchFormState.status"
							:options="statusOptions"
							placeholder="请选择状态"
							allow-clear
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => formRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" :loading="exporting" @click="exportFile()">导出</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'promoteType'">
					<a-tag color="red">
						{{ $TOOL.dictTypeData('PROMOTE_TYPE', record.promoteType + '') }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'rankType'">
					<a-tag color="blue">
						{{ $TOOL.dictTypeData('PROMOTE_RANK_TYPE', record.rankType + '') }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag v-if="record.status === 1" color="blue">
						{{ $TOOL.dictTypeData('PROMOTE_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-else-if="record.status === 4" color="red">
						{{ $TOOL.dictTypeData('PROMOTE_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-else>
						{{ $TOOL.dictTypeData('PROMOTE_STATUS', record.status + '') }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'promoteDate'">
					<div>
						<div>开始日期：{{ dayjs(record.startTime).format('YYYY-MM-DD') }}</div>
						<div>结束日期：{{ dayjs(record.endTime).format('YYYY-MM-DD') }}</div>
					</div>
				</template>
				<template v-if="column.dataIndex === 'goodInfo'">
					<div>
						<div>商品编码：{{ record.itemCode }}</div>
						<div>商品名称：{{ record.goodName }}</div>
						<div>品类：{{ record.categoryCode + '-' + record.categoryName }}</div>
					</div>
				</template>
				<template v-if="column.dataIndex === 'storeCode'">
					{{ $TOOL.tableData('ORG_CATEGORY', record.storeCode, 'code') }}
				</template>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="dailySale">
	import { message } from 'ant-design-vue'
	import biApi from '@/api/bi/biApi'
	import tool from '@/utils/tool'
	import dayjs from 'dayjs'
	let searchFormState = reactive({
		promoteStartTime: dayjs(new Date()).subtract(7, 'day'),
		saleStartTime: dayjs(new Date()).subtract(7, 'day'),
		promoteEndTime: dayjs(new Date()).subtract(1, 'day'),
		saleEndTime: dayjs(new Date()).subtract(1, 'day')
	})
	const table = ref(null)
	const exporting = ref(false)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: true }
	const columns = [
		{
			title: '促销标题',
			dataIndex: 'title'
		},
		{
			title: '促销类型',
			dataIndex: 'promoteType',
			align: 'center'
		},
		{
			title: '促销范围',
			dataIndex: 'rankType',
			align: 'center'
		},
		{
			title: '促销状态',
			dataIndex: 'status',
			align: 'center'
		},
		{
			title: '活动日期',
			dataIndex: 'promoteDate',
			align: 'left'
		},
		{
			title: '商品信息',
			dataIndex: 'goodInfo',
			align: 'left'
		},
		{
			title: '门店',
			dataIndex: 'storeCode',
			align: 'left'
		},
		{
			title: '销售金额',
			dataIndex: 'totalAmount',
			align: 'right',
			width: 100
		},
		{
			title: '销售数量',
			dataIndex: 'totalItemCount',
			align: 'right',
			width: 100
		},
		{
			title: '客流量',
			dataIndex: 'totalCustomers',
			align: 'right',
			width: 100
		},
		{
			title: '当前库存',
			dataIndex: 'stock',
			align: 'right'
		},
		{
			title: '销售范围',
			dataIndex: 'saleRange',
			align: 'center'
		}
	]
	let selectedRowKeys = ref([])
	let promoteTypeOptions = tool.dictTypeList('PROMOTE_TYPE').map((item) => {
		return {
			value: item['dictValue'],
			label: item['name']
		}
	})
	let statusOptions = tool.dictTypeList('PROMOTE_STATUS').map((item) => {
		return {
			value: item['dictValue'],
			label: item['name']
		}
	})
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	// 列表数据
	const loadData = (parameter) => {
		const params = Object.assign(parameter, searchFormState)
		if (params.promoteStartTime) {
			params.promoteStartTime = params.promoteStartTime.format('YYYY-MM-DD')
		}
		if (params.saleStartTime) {
			params.saleStartTime = params.saleStartTime.format('YYYY-MM-DD')
		}
		if (params.promoteEndTime) {
			params.promoteEndTime = params.promoteEndTime.add(1, 'day').format('YYYY-MM-DD')
		}
		if (params.saleEndTime) {
			params.saleEndTime = params.saleEndTime.add(1, 'day').format('YYYY-MM-DD')
		}
		return biApi.pagePromoteSale(params).then((res) => {
			return res
		})
	}

	const exportFile = () => {
		exporting.value = true
		const params = Object.assign({}, searchFormState)
		if (params.promoteStartTime) {
			params.promoteStartTime = params.promoteStartTime.format('YYYY-MM-DD')
		}
		if (params.saleStartTime) {
			params.saleStartTime = params.saleStartTime.format('YYYY-MM-DD')
		}
		if (params.promoteEndTime) {
			params.promoteEndTime = params.promoteEndTime.add(1, 'day').format('YYYY-MM-DD')
		}
		if (params.saleEndTime) {
			params.saleEndTime = params.saleEndTime.add(1, 'day').format('YYYY-MM-DD')
		}
		params.pageNo = null
		biApi.exportExcel('bi/promoteReport/export', '促销销售分析' + Date.now(), params).finally((_) => {
			exporting.value = false
		})
	}
</script>

<style scoped></style>
